﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Newtonsoft.Json;
using YJTMedicalAideManage.Cores.Models;
using YJTMedicalAideManage.Cores.Servers;

namespace YJTMedicalAideManage.Cores.Globals
{
    public class GlobalApp
    {
        private static GlobalApp _instance;

        private static readonly object _lock = new object();
        private readonly ApiService _apiService;
        public string Manager_Id { get; set; }
        public string Ass_Id { get; set; }
        public string Role_Id { get; set; }
        public string Scale { get; set; }
        public string Token { get; set; }
        public string BaseUrl { get; set; }

        public string ClinicId { get; set; }
        public string OperatorId { get; set; }

        public string ClinicName { get; set; } = "";


        public List<adddress> ProvinceList { get; private set; }
        public List<adddress> CityList { get; private set; }
        public List<adddress> AreaList { get; private set; }
        public string RemoveChineseCharacters(string input)
        {
            // 正则表达式匹配中文字符
            string pattern = @"[\u4e00-\u9fa5]";

            // 使用正则表达式替换中文字符为空字符串
            return Regex.Replace(input, pattern, "");
        }
        public void LoadDataFromJson(string filePath)
        {
            var jsonData = File.ReadAllText(filePath);
            
        }
        private GlobalApp()
        {
            ProvinceList = new List<adddress>();
            CityList = new List<adddress>();
            AreaList = new List<adddress>();
            _apiService = new ApiService();
        }
        public async Task GetAdrss(string name, string id) 
        {
            if (name == "city")
            {
                var Cdata = await _apiService.GetAllCityAsync(id);
                foreach (var item in Cdata.data)
                {
                    CityList.Add(new adddress() { cityName = item.cityName, cityId = item.cityId });
                }

            }
            else
            {
                var Adata = await _apiService.GetAllAreaAsync(id);


                foreach (var item in Adata.data)
                {
                    AreaList.Add(new adddress() { areaName = item.areaName, areaId = item.areaId });
                }
            }
        }

        public static GlobalApp Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_lock)
                    {
                        if (_instance == null)
                        {
                            _instance = new GlobalApp();
                        }
                    }
                }
                return _instance;
            }
        }
        public bool IsValidPassword(string input)
        {
            // 正则表达式：至少一个大写字母，一个小写字母，一个数字，且长度至少为8
            string pattern = @"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[A-Za-z\d]{8,}$";

            // 使用正则表达式验证输入
            return Regex.IsMatch(input, pattern);
        }

        public bool IsValidPhoneNumber(string input)
        {
            // 正则表达式：确保输入是11位数字
            string pattern = @"^\d{11}$";
            return Regex.IsMatch(input, pattern);
        }
        public void Run()
        {
            Console.WriteLine("GlobalApp 正在运行...");
        }
    }
}
